﻿
namespace Admin.NET.Application.Service;

/// <summary>
/// 用于打印的模板的设计和SQL运维
/// </summary>
public class BasPrintDraftService : BaseService<BasPrintDraft>
{
    DataBaseManager _dbManager;

    public BasPrintDraftService(IServiceScopeFactory scopeFactory, DataBaseManager dbManager) : base(scopeFactory)
    {
        _dbManager = dbManager;
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    public async Task<SqlSugarPagedList<BasPrintDraftOutput>> Page(BasPrintDraftPageInput input)
    {
        return await Queryable
           .WhereIF(input.Keyword.HasValue(), it => it.title.Contains(input.Keyword))
           .WhereIF(input.category.HasValue(), it => it.category == input.category)
           .Select<BasPrintDraftOutput>()
           .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public BasPrintDraftOutput Get(long id)
    {
        return GetById<BasPrintDraftOutput>(id);
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    public long Add(BasPrintDraftInput input)
    {
        return AddWithInput(input);
    }

    /// <summary>
    /// 更新成功后返回true
    /// </summary>
    public bool Update(BasPrintDraftInput input)
    {
        return UpdateWithInput(input);
    }

    /// <summary>
    /// 仅查询SQL1
    /// </summary>
    public async Task<BasPrintGetDataAndTemplateOutput> QueryOnly(BasPrintDraftQueryInput input)
    {
        var entity = await Queryable.Where(it => it.Id == input.Id).FirstAsync();
        if (entity == null)
        {
            throw Oops.Oh("草稿不存在");
        }

        List<SugarParameter> parameter = new List<SugarParameter>();
        parameter.Add(new SugarParameter("@p1", input.P1));
        parameter.Add(new SugarParameter("@p2", input.P2));
        parameter.Add(new SugarParameter("@p3", input.P3));

        var t1 = _dbManager.Query2List(entity.sql1, parameter);

        return new BasPrintGetDataAndTemplateOutput()
        {
            Id = entity.Id,
            Template = entity.template,
            Data = new BasPrintDataOutput()
            {
                T1 = t1,
            }
        };
    }

    /// <summary>
    /// 查询数据和模板, 用于Web打印
    /// </summary>
    public async Task<BasPrintGetDataAndTemplateOutput> QueryDataAndTemplate(BasPrintDraftQueryInput input)
    {
        var entity = await Queryable.Where(it => it.Id == input.Id).FirstAsync();
        if (entity == null)
        {
            throw Oops.Oh("草稿不存在");
        }

        List<SugarParameter> parameter = new List<SugarParameter>();
        parameter.Add(new SugarParameter("@p1", input.P1));
        parameter.Add(new SugarParameter("@p2", input.P2));
        parameter.Add(new SugarParameter("@p3", input.P3));

        var t1 = _dbManager.Query2List(entity.sql1, parameter);
        var t2 = _dbManager.Query2List(entity.sql2, parameter);
        var t3 = _dbManager.Query2List(entity.sql3, parameter);
        var t4 = _dbManager.Query2List(entity.sql4, parameter);
        var t5 = _dbManager.Query2List(entity.sql5, parameter);

        return new BasPrintGetDataAndTemplateOutput()
        {
            Id = entity.Id,
            Template = entity.template,
            Data = new BasPrintDataOutput()
            {
                T1 = t1,
                T2 = t2,
                T3 = t3,
                T4 = t4,
                T5 = t5,
            }
        };
    }

    /// <summary>
    /// Tabulator 的列格式 [{ title: 'Id', field: 'Id', type: 'text' },{ title: 'TenantId', field: 'TenantId', type: 'text' },]
    /// </summary>
    public async Task<BasPrintGetDataAndTemplateOutput> Query4Tabulator(BasPrintDraftQueryInput input)
    {
        var entity = await Queryable.Where(it => it.Id == input.Id).FirstAsync();
        if (entity == null)
        {
            throw Oops.Oh("草稿不存在");
        }

        List<SugarParameter> parameter = new List<SugarParameter>();
        parameter.Add(new SugarParameter("@p1", input.P1));
        parameter.Add(new SugarParameter("@p2", input.P2));
        parameter.Add(new SugarParameter("@p3", input.P3));

        var t1 = _dbManager.Query2Tabulator(entity.sql1, parameter);

        return new BasPrintGetDataAndTemplateOutput()
        {
            Id = entity.Id,
            Template = entity.template,
            Data = new BasPrintDataOutput()
            {
                Columns = t1.columns,
                T1 = t1.data,
            }
        };
    }
}
